package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangxin
 * @description
 * @date 2020/12/3
 */
@RestController
public class StockController {

    @Autowired
    private StockService stockService;

    // 保存报损单（报损单号,前端生成）
    // http://localhost:8080/damageListGoods/save?damageNumber=BS1605766644460
    @PostMapping("/damageListGoods/save")
    @SuppressWarnings("rawtypes")
    public ServiceVO saveDamageListAndGoods(DamageList damageList, String damageListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        stockService.saveDamageListAndGoods(user, damageList, damageListGoodsStr);
        return new ServiceVO(100, "请求成功");
    }

    // 新增报溢单
    // http://localhost:8080/overflowListGoods/save?overflowNumber=BY1605767033015（报溢单号）
    @PostMapping("/overflowListGoods/save")
    @SuppressWarnings("rawtypes")
    public ServiceVO saveOverflowListAndGoods(OverflowList overflowList, String overflowListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        stockService.saveOverflowListAndGoods(user, overflowList, overflowListGoodsStr);
        return new ServiceVO(100, "请求成功");
    }

    // 查询所有当前库存量小于库存下限的商品信息
    // http://localhost:8080/goods/listAlarm
    @PostMapping("/goods/listAlarm")
    public Map<String, Object> getAllOutOfStockGoods() {
        List<Goods> outOfStockGoodsList = stockService.getAllOutOfStockGoods();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("rows", outOfStockGoodsList);
        return resultMap;
    }

    // 报损单查询
    // http://localhost:8080/damageListGoods/list
    @PostMapping("/damageListGoods/list")
    public Map<String, Object> getDamageListByTime(String sTime, String eTime) {
        List<DamageList> damageList = stockService.getDamageListByTime(sTime, eTime);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("rows", damageList);
        return resultMap;
    }

    // 查询报损单商品信息
    // http://localhost:8080/damageListGoods/goodsList
    @PostMapping("/damageListGoods/goodsList")
    public Map<String, Object> getDamageListGoodsById(Integer damageListId) {
        List<DamageListGoods> damageListGoodsList = stockService.getDamageListGoodsById(damageListId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("rows", damageListGoodsList);
        return resultMap;
    }

    // 报溢单查询
    // http://localhost:8080/overflowListGoods/list
    @PostMapping("/overflowListGoods/list")
    public Map<String, Object> getOverflowListByTime(String sTime, String eTime) {
        List<OverflowList> overflowList = stockService.getOverflowListByTime(sTime, eTime);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("rows", overflowList);
        return resultMap;
    }

    // 报溢单商品信息
    // http://localhost:8080/overflowListGoods/goodsList
    @PostMapping("/overflowListGoods/goodsList")
    public Map<String, Object> getOverflowListGoodsById(Integer overflowListId) {
        List<OverflowListGoods> overflowListGoodsList = stockService.getOverflowListGoodsById(overflowListId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("rows", overflowListGoodsList);
        return resultMap;
    }
}
